package accs.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import accs.gui.logic.CheckerTask;
import accs.gui.logic.ProgressListener;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class TaskPanel extends JPanel {
	private CheckerTask task=null;
	private JPanel actionPanel;
	private JButton btnStart;
	private JButton btnStop;
	private ProgressPanel progressPanel;
	private JLabel infoLabel;
	
	private long elapsedTimeUntilPause=0;
	private long lastStartTimestamp=0;
	private JLabel titleLabel;
	
	public TaskPanel(CheckerTask checkerTask) {
		this.task=checkerTask;
		
		task.addProgressListener(new ProgressListener() {
			public void progressUpdate(int working, int notWorking, int total) {
				progressPanel.setParts(working, notWorking, total);				
				
				long elapsedTime=elapsedTimeUntilPause+System.currentTimeMillis()-lastStartTimestamp;
				long elapsedItems=working+notWorking;
				long remainingItems=total-elapsedItems;
				long remainingTime=remainingItems*elapsedTime/elapsedItems;
				
				infoLabel.setText("Working: "+working+" Remaining Time: "+formatDuration(remainingTime));
				
				if(working+notWorking==total) {
					btnStart.setEnabled(false);
					btnStop.setEnabled(false);
					try {
						task.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			public void infoTextUpdate(String text) {
				titleLabel.setText(text);
			}
		});
		
		setLayout(new BorderLayout(0, 0));
		
		progressPanel = new ProgressPanel();
		progressPanel.setParts(0,0,task.getTotal());
		add(progressPanel, BorderLayout.CENTER);
		
		actionPanel = new JPanel();
		add(actionPanel, BorderLayout.EAST);
		actionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnStart = new JButton("");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnStart.setEnabled(false);
				btnStop.setEnabled(true);
				
				lastStartTimestamp=System.currentTimeMillis();
				
				task.start();
			}
		});
		btnStart.setIcon(new ImageIcon(TaskPanel.class.getResource("/accs/gui/img/play.png")));
		actionPanel.add(btnStart);
		
		btnStop = new JButton("");
		btnStop.setEnabled(false);
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnStart.setEnabled(true);
				btnStop.setEnabled(false);
				
				elapsedTimeUntilPause+=System.currentTimeMillis()-lastStartTimestamp;
				
				task.stop();
			}
		});
		btnStop.setIcon(new ImageIcon(TaskPanel.class.getResource("/accs/gui/img/pause.png")));
		btnStop.setEnabled(false);
		actionPanel.add(btnStop);
		
		infoLabel = new JLabel("Not yet started");
		add(infoLabel, BorderLayout.SOUTH);
		
		setMaximumSize(new Dimension(Integer.MAX_VALUE,10));
		
		titleLabel = new JLabel(task.getTitle());
		add(titleLabel, BorderLayout.NORTH);
	}
	
	private String formatDuration(long time) {
		time/=1000;
		long hours=time/3600;
		time%=3600;
		long minutes=time/60;
		time%=60;
		if(hours==0 && minutes==0) return time+"s";
		else if(hours==0) return minutes+"m "+time+"s";
		else return hours+"h "+minutes+"m "+time+"s";
	}
}